package com.hut.oa.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.hut.oa.common.StringUtil;
import com.hut.oa.model.bean.Dept;
import com.hut.oa.model.bean.DeptOffice;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.user.DeptDao;
import com.hut.oa.model.dao.user.DeptOfficeDao;
import com.hut.oa.model.dao.user.UserDao;

@Service
@Transactional(readOnly=true)
public class UserService {
	
	@Autowired
	DeptDao dd;
	
	@Autowired
	DeptOfficeDao dod;
	
	@Autowired
	UserDao ud;
	
	/**
	 * 交换两个部门的层级
	 * @param thisId
	 * @param otherId
	 */
	@Transactional
	public void updateGrade(Long thisId,Long otherId){
		Dept thisDept=dd.findById(thisId);
		Dept otherDept=dd.findById(otherId);
		dd.updateGrade(otherDept.getGrade(), thisId);
		dd.updateGrade(thisDept.getGrade(), otherId);
	}
	
	/**
	 * 获取各部门职位最高的
	 * @param dept
	 * @return
	 */
	public List<DeptOffice> getOffice(Dept dept){
		List<DeptOffice> offices=dod.findLeader(dept);
		this.setOfficeDirectChildren(offices,dept);
		return offices;
	}
	/**
	 * 递归设置直接下属
	 * @param offices
	 */
	public void setOfficeDirectChildren(List<DeptOffice> offices,Dept dept){
		for (DeptOffice deptOffice : offices) {
			List<DeptOffice> children=dod.findByLeaderAndDept(deptOffice,dept);
			deptOffice.setChildren(children);
			this.setOfficeDirectChildren(children,dept);
		}
	}
	
	/**
	 * 组合不同条件查询
	 * @param dept
	 * @param key
	 * @param pageable
	 * @return
	 */
	public Page<User> findByContition(Long deptId,String key,Pageable pageable){
		Page<User> users;
		if(StringUtil.isNull(key) && StringUtil.isNull(deptId)){
			users=ud.findAll(pageable);
		}else if(StringUtil.isNull(deptId)){
			users=ud.findByRealNameLikeOrTelLike(key,pageable);
		}else if(StringUtil.isNull(key)){
			users=ud.findByDept(dd.findById(deptId),pageable);
		}else{
			users=ud.findByRealNameOrTelAndDept(key, dd.findById(deptId), pageable);
		}
		return users;
		
	}
	
	/**
	 * 修改用户帐号名和密码
	 * @param name
	 * @param pwd
	 * @param userId
	 */
	@Transactional
	public void updateAccount(String name,String pwd,Long userId){
		ud.updateAccount(name, pwd, userId);
	}
	
	/**
	 * 修改账号名、密码、皮肤
	 * @param name
	 * @param password
	 * @param skin
	 * @param id
	 */
	@Transactional
	public void updateSystem(String name,String password,String skin,Long id){
		ud.updateSystem(name, password, skin, id);
	}
	
	/**
	 * 修改基本信息
	 */
	@Transactional
	public void updateBase(Long id,String tel,Date birDay,String address,String bank,String email){
		ud.updateBase(tel, email, address, bank, birDay, id);
	}
	
}
